package threadedbinarytree;

/**
 * Created by IntelliJ
 * Author:熊磊
 */
public class ThreadedBinaryTree {
    private HeroNode root;
    //为了线索化，我们需要一个前驱节点
    private HeroNode pre=null;

    public void setRoot(HeroNode root) {
        this.root = root;
    }


    public static void main(String[] args) {
        HeroNode root = new HeroNode(1, "tom");
        HeroNode node2 = new HeroNode(3, "jack");
        HeroNode node3 = new HeroNode(6, "smith");
        HeroNode node4 = new HeroNode(8, "mary");
        HeroNode node5 = new HeroNode(10, "king");
        HeroNode node6 = new HeroNode(14, "dim");
        ThreadedBinaryTree tree = new ThreadedBinaryTree();
        tree.setRoot(root);
        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);
        tree.threadedNodes();

        //测试中序线索化
        //HeroNode left = node5.getLeft();
        //int leftType = node5.getLeftType();
        //HeroNode right = node5.getRight();
        //int rightType = node5.getRightType();
        //System.out.println(left+""+leftType);
        //System.out.println(right+""+rightType);
        System.out.println(node3.getRightType());
        System.out.println("======遍历线索化二叉树======");
        tree.threadedList();

    }
    //遍历线索化二叉树的方法
    public void threadedList(){
        HeroNode node = root;
        while (node!=null){
            while (node.getLeftType()==0){
                node=node.getLeft();
            }
            System.out.println(node);
            while (node.getRightType()==1){
                node=node.getRight();
                System.out.println(node);
            }
            node=node.getRight();
        }
    }
    //线索化入口
    public void threadedNodes(){
        this.threadedNodes(root);
    }

    /**
     *
     * @param node 要进行线索化的节点
     */
    public void threadedNodes(HeroNode node){
        if (node==null){
            return;
        }
        //线索化左子树
        threadedNodes(node.getLeft());

        //处理前驱节点,第一个遍历的节点的pre==null
        if (node.getLeft()==null){
            //当前节点的左指针指向pre
            node.setLeft(pre);
            node.setLeftType(1);
        }

        //处理pre的后继节点
        if (pre!=null&&pre.getRight()==null){
           pre.setRight(node);
           pre.setRightType(1);
        }
        //每处理一个节点后，让当前节点是下一个节点的前驱节点
        pre=node;

        //线索化右子树
        threadedNodes(node.getRight());
    }

    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    public void infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    public HeroNode preSearch(int no) {
        if (this.root != null) {
            return root.preSearch(no);
        } else {
            return null;
        }
    }

    public HeroNode infixSearch(int no) {
        if (this.root != null) {
            return root.infixSearch(no);
        } else {
            return null;
        }
    }

    public HeroNode postSearch(int no) {
        if (this.root != null) {
            return root.postSearch(no);
        } else {
            return null;
        }
    }

    public void delNode(int no) {
        if (root != null) {
            if (root.getNo() == no) {
                root = null;
            } else {
                root.deleteNode(no);
            }
        } else {
            System.out.println("该树为空");
        }
    }
}
class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    //如果leftType==0表示指向的是左子树,1表示前驱节点
    private int leftType;
    private int rightType;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public void preOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }

    public HeroNode preSearch(int no) {
        if (this.no == no) {
            return this;
        }
        if (this.left != null) {
            if (this.left.preSearch(no) != null) {
                return this.left.preSearch(no);
            }
        }
        if (this.right != null) {
            return this.right.preSearch(no);
        }
        return null;
    }

    public HeroNode infixSearch(int no) {
        if (this.left != null) {
            if (this.left.preSearch(no) != null) {
                return this.left.preSearch(no);
            }
        }
        if (this.no == no) {
            return this;
        }
        if (this.right != null) {
            return this.right.preSearch(no);
        }
        return null;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    public HeroNode postSearch(int no) {
        if (this.left != null) {
            if (this.left.preSearch(no) != null) {
                return this.left.preSearch(no);
            }
        }
        if (this.right != null) {
            if (this.right.preSearch(no) != null) {
                return this.right.preSearch(no);
            }
        }
        if (this.no == no) {
            return this;
        }

        return null;
    }

    //删除结点,会删除所有编号=no的子树
    public void deleteNode(int no) {
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.no == no) {
            this.left = null;
            return;
        }
        if (this.left != null) {
            this.left.deleteNode(no);
        }
        if (this.right != null) {
            this.right.deleteNode(no);
        }
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    public int getNo() {
        return no;
    }

    public String getName() {
        return name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public HeroNode getRight() {
        return right;
    }
}